<?php

declare(strict_types=1);

namespace app\api\controller;

use app\BaseController;
use app\common\model\Product\Product as ProductModel;
use app\common\model\Product\Type as TypeModel;

class Home extends BaseController
{

    public function index()
    {
        $TypeList = TypeModel::limit(8)->order('weigh', 'desc')->select();
        $RecomList = ProductModel::where(['flag' => 3])->limit(6)->select();
        $HotList = ProductModel::where(['flag' => 2])->limit(6)->select();
        $NewList = ProductModel::where(['flag' => 1])->limit(6)->select();
        $data = [
            'TypeList' => $TypeList,
            'RecomList' => $RecomList,
            'HotList' => $HotList,
            'NewList' => $NewList
        ];
        $this->success('查询成功', null, $data);
    }
    // 搜索框
    public function search()
    {
        $proname = $this->request->param('proname');
        // halt($proname);
        $where = [];
        if ($proname) {
            $where[] = ['name', 'like', "%$proname%"];
        }
        $SearchData = ProductModel::where($where)->select();

        if ($SearchData === FALSE) {
            $this->error('查询失败');
        }
        
        $this->success('查询成功', null, $SearchData);
    }
}
